var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    publishid: '',
    usid: '',
    clocks: [],
    user_type: 1,
    pj: {},
    visible1: false,
    evaluate: '',
    score: 0,
    percent: 0,
    prompt: {
      hidden: 0,
      icon: '../../../assets/images/iconfont-order-default.png',
      title: '暂无数据',
      text: '没有任何打卡记录',
      gurl: '../../pages/home/home',
      gtype: "switchTab"
    },
    oclocktype: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const publishid = options.publishid
    const usid = options.usid
    const percent = options.percent
    var user_type = wx.getStorageSync('user_type');
    this.setData({
      publishid: publishid,
      usid: usid,
      user_type: user_type,
      percent: percent
    })
    this.loadOclockData()
     //查询评价数据
    this.loadEvaluate()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  loadOclockData(){
    const that = this
    const publishid  = this.data.publishid 
    const usid  = this.data.usid 
    wx.showLoading({
      title: '加载中',
    })
    app.wxget({
      url: 'issuedWorks/loadOclockData.html?publishid='+publishid+'&usid=' + usid,
      success: function (res) {
        var clocks = res.data
        wx.hideLoading()
        let oclocktype = clocks[0].OCLOCKTYPE
        that.setData({
          oclocktype: oclocktype,
          clocks: clocks
        })
      }
    })
  },

  viewResource (e) {
    const index = e.currentTarget.dataset.index
    const tindex = e.currentTarget.dataset.tindex
    const findex = e.currentTarget.dataset.findex
    
    var clocks = this.data.clocks
    const clock = clocks[index].datas
    const files = clock[tindex].files
    var resource = [] 
    for (var i = 0; i < files.length; i++) {
      const source = {
        url: files[i].INFORMATION,
        type: files[i].FILETYPE
      }
      resource.push(source)
    }
    wx.previewMedia({
      sources: resource,
      current: findex
    })
  },


  loadEvaluate () {
    const that = this
    const publishid  = this.data.publishid 
    const usid  = this.data.usid 
    app.wxget({
      url: 'issuedWorks/loadEvaluate.html?publishid='+publishid+'&usid=' + usid,
      success: function (res) {
        if (res.data) {
          var pj = res.data
          that.setData({
            pj: pj
          })
        }
      }
    })
  },


  openEvluate () {
    this.setData({
      visible1: true
    })
  },

  handleClose1() {
    this.setData({
      visible1: false
    })
  },

  scoreChange(e) {
    const index = e.detail.index;
    this.setData({
        'score' : index
    })
  },

  inputEvaluate (e) {
    this.setData({
      evaluate : e.detail.value
    })
  },

  submitEvluate() {
    const that = this
    //提交评论
    const publishid  = this.data.publishid
    const usid  = this.data.usid
    const evaluate  = this.data.evaluate
    const score  = this.data.score
    if (!evaluate) {
      wx.showToast({
        title: '请填写评价',
        icon: 'error',
        duration: 2000
      })
      return false
    }
    wx.showLoading({
      title: '提交中',
    })
    const addusid = wx.getStorageSync('usid')
    app.wxpost({
      url: 'issuedWorks/insertClockScore.html',
      data: {
        usid: usid,
        publishid: publishid,
        evaluate: evaluate,
        score: score,
        addusid: addusid
      },
      success: function (res) {
        wx.hideLoading()
        that.setData({
          visible1: false
        })
        that.loadEvaluate()
      }
    })
  }

})